package fr.afcepf.ai89.esharing.managedbean;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import javax.faces.event.ActionEvent;
import javax.faces.model.SelectItem;

import fr.afcepf.ai89.esharing.entity.Aem;
import fr.afcepf.ai89.esharing.entity.CategorieAem;
import fr.afcepf.ai89.esharing.entity.FrCodePostalAsso;
import fr.afcepf.ai89.esharing.entity.FrCommune;
import fr.afcepf.ai89.esharing.entity.FrCommunePK;
import fr.afcepf.ai89.esharing.entity.FrDepartement;
import fr.afcepf.ai89.esharing.entity.FrRegion;
import fr.afcepf.ai89.esharing.entity.Valeur;
import fr.afcepf.ai89.esharing.metier.BusinessApprovisionnerCriteres;
import fr.afcepf.ai89.esharing.metier.BusinessRechercheAem;
import fr.afcepf.ai89.esharing.metier.IBusinessApprovisionnerCriteres;
import fr.afcepf.ai89.esharing.metier.IBusinessRechercheAem;

public class ResultatRechercheManagedBean {
	
	private IBusinessApprovisionnerCriteres busApp = new BusinessApprovisionnerCriteres();
	private IBusinessRechercheAem busRec = new BusinessRechercheAem();
	
	private Date dateDe;
	private Date dateA;
	private SelectItem[] regions;
	private Integer regionId;
	private SelectItem[] departements;
	private String departementId;
	private SelectItem[] communes;
	private FrCommune commune;
	private String lienRegion;
	private String lienDepartement;
	private String lienCommuneVille;
	private List<CategorieAem> categories;
	private List<AEMView> aems; 
	private String motCleRecherche ="";
	
	private IndexManagedBean mbIndex;
	
	private SelectItem[] codePostaux;
	private Integer codePostalId;
	private FrCommunePK communeId = new FrCommunePK();
	private int communeCode = 0;
	
	private Integer categorieId;
	private Double valMax;
	private int aemAficher;
	
//	private String bannGeo = "";
	
	public Integer getCategorieId() {
		return categorieId;
	}

	public void setCategorieId(Integer categorieId) {
		this.categorieId = categorieId;
	}

	public Double getValMax() {
		return valMax;
	}

	public void setValMax(Double valMax) {
		this.valMax = valMax;
	}

//	public String getBannGeo() {
//		
//		if (regionId != null && regionId > 0) {
//			
//			FrRegion reg = busApp.getRegion(regionId);
//			
//			//System.out.println("********REGION : " + reg.getNomRegion() + "********");
//			
//			bannGeo = busApp.getRegion(regionId).getNomRegion();
//			
//			if (departementId != null && departementId != "") {
//				
//				bannGeo = bannGeo + " → " + busApp.getDept(departementId).getNomDepartement();
//				
//			}
//			
//		}
//		else {
//		
//			if (departementId != null && departementId != "") {
//				
//				FrDepartement dept = busApp.getDept(departementId);
//				bannGeo = dept.getFrRegion().getNomRegion() + " → " + dept.getNomDepartement();
//				
//			}
//			else {
//				
//				bannGeo = "";
//				
//			}
//			
//		}
//		
//		return bannGeo;
//	}

//	public void setBannGeo(String bannGeo) {
//		this.bannGeo = bannGeo;
//	}

	public Integer getCodePostalId() {
		return codePostalId;
	}

	public void setCodePostalId(Integer codePostalId) {
		this.codePostalId = codePostalId;
	}

	public FrCommunePK getCommuneId() {
		return communeId;
	}

	public void setCommuneId(FrCommunePK communeId) {
		this.communeId = communeId;
	}

	public int getCommuneCode() {
		return communeCode;
	}

	public void setCommuneCode(int communeCode) {
		this.communeCode = communeCode;
	}

	public IndexManagedBean getMbIndex() {
		return mbIndex;
	}

	public void setMbIndex(IndexManagedBean mbIndex) {
		this.mbIndex = mbIndex;
	}

	public void setDepartementId(String departementId) {
		this.departementId = departementId;
	}

	public String getMotCleRecherche() {
		return motCleRecherche;
	}

	public void setMotCleRecherche(String motCleRecherche) {
		this.motCleRecherche = motCleRecherche;
	}

	public String aller(){
		return "";
	}
	
	public String raffiner(){
		return "";
	}

	public Date getDateDe() {
		return dateDe;
	}

	public void setDateDe(Date dateDe) {
		this.dateDe = dateDe;
	}

	public Date getDateA() {
		return dateA;
	}

	public void setDateA(Date dateA) {
		this.dateA = dateA;
	}

	public SelectItem[] getRegions() {
		return regions;
	}

	public void setRegions(SelectItem[] regions) {
		this.regions = regions;
	}

	public Integer getRegionId() {
		return regionId;
	}

	public void setRegionId(Integer regionId) {
		this.regionId = regionId;
	}

	public SelectItem[] getDepartements() {
		return departements;
	}

	public void setDepartements(SelectItem[] departements) {
		this.departements = departements;
	}

	public String getDepartementId() {
		return departementId;
	}

	public void setDepartement(String departementId) {
		this.departementId = departementId;
	}

	public SelectItem[] getCodePostaux() {
		return codePostaux;
	}

	public void setCodePostaux(SelectItem[] codePostaux) {
		this.codePostaux = codePostaux;
	}

	public SelectItem[] getCommunes() {
		return communes;
	}

	public void setCommunes(SelectItem[] communes) {
		this.communes = communes;
	}

	public FrCommune getCommune() {
		return commune;
	}

	public void setCommune(FrCommune commune) {
		this.commune = commune;
	}

	public String getLienRegion() {
		return lienRegion;
	}

	public void setLienRegion(String lienRegion) {
		this.lienRegion = lienRegion;
	}

	public String getLienDepartement() {
		return lienDepartement;
	}

	public void setLienDepartement(String lienDepartement) {
		this.lienDepartement = lienDepartement;
	}

	public String getLienCommuneVille() {
		return lienCommuneVille;
	}

	public void setLienCommuneVille(String lienCommuneVille) {
		this.lienCommuneVille = lienCommuneVille;
	}

	public List<CategorieAem> getCategories() {
		return categories;
	}

	public void setCategories(List<CategorieAem> categories) {
		this.categories = categories;
	}

	public List<AEMView> getAems() {
		
		if (mbIndex != null && mbIndex.isOnThisPage() == true) {
			
			departementId = mbIndex.getDepartementId();
			motCleRecherche = mbIndex.getMotCleRecherche();
			dateDe = mbIndex.getDateDebut();
			dateA = mbIndex.getDateFin();
			
			mbIndex.setOnThisPage(false);
			
		}

		rechercher();
		return aems;
		
	}

	public void setAems(List<AEMView> aems) {
		this.aems = aems;
	}
	
	public ResultatRechercheManagedBean() {
		
		loadRegions();
		loadDepartements();
				
	}
	
	public String valueChangeRegion() {

		if (regionId != null) {
			
			//System.out.println("***********************");
			//System.out.println("***********************");
			//System.out.println(regionId);
			//System.out.println("***********************");
			//System.out.println("***********************");
			
			FrRegion region = new FrRegion();
			region.setIdRegion(regionId);
			loadDepartements(region);
			
		}
		
		else {
			
			loadDepartements();
			
		}
		
//		viderCodesPostaux();
//		viderCommunes();
		
		return "";
		
	}
	
	private void loadRegions() {

		List<FrRegion> liste = busApp.getRegions();
		Collections.sort(liste, new FrRegionComparateur());
		int i = 1;
		
		regions = new SelectItem[liste.size() + 1];
		regions[0] = new SelectItem();
		regions[0].setLabel("--");
		regions[0].setValue(null);
		
		for (FrRegion region : liste) {
			
			regions[i] = new SelectItem();
			regions[i].setLabel(region.getNomRegion());
			regions[i].setValue(region.getIdRegion());
			i++;
			
		}
		
	}
	
	public String valueChangeDepartement() {

		if (departementId != null) {
			
			FrDepartement dept = new FrDepartement();
			dept.setIdDepartement(departementId);
//			dept = busApp.getDept(dept.getIdDepartement());
//			
//			regionId = (Integer) (int) dept.getFrRegion().getIdRegion();
//			departementId = dept.getIdDepartement();
			
//			loadCodesPostaux(dept);
//			loadCommunes(dept);
			
		}
		else {
			
//			viderCodesPostaux();
//			viderCommunes();
			
		}
		
		return "";
		
	}

	private void listerDepartements(List<FrDepartement> liste) {
		
		int i = 1;
		
		departements = new SelectItem[liste.size() + 1];
		departements[0] = new SelectItem();
		departements[0].setLabel("Dépt.");
		departements[0].setValue("");
		
		for (FrDepartement dept : liste) {
			
//			//System.out.println(dept.getIdDepartement() + " " + dept.getNomDepartement());
			departements[i] = new SelectItem();
			departements[i].setLabel(String.valueOf(dept.getIdDepartement()) + " " + dept.getNomDepartement());
			departements[i].setValue(dept.getIdDepartement());
			i++;
			
		}
		
	}
	
	private void loadDepartements() {
		
		List<FrDepartement> liste = busApp.getDepts();
		listerDepartements(liste);
		
	}
	
	private void loadDepartements(FrRegion region) {
		
		List<FrDepartement> liste = busApp.getDepts(region);
		listerDepartements(liste);
		
	}
	
	public void valueChangeVille() {
		
		if (communeCode > 0) {
			
			FrCommune com = new FrCommune();
			com.setId(new FrCommunePK());
//			com.getId().setCodeCommune(communeCode);
//			com.getId().setIdDepartement(departementId);
//			loadCodesPostaux(com);
			
		}
		
	}

	private void listerCommunes(List<FrCommune> liste) {
		
		int i = 1;
		
		Collections.sort(liste, new FrCommuneComparateur());
		
		communes = new SelectItem[liste.size() + 1];
		communes[0] = new SelectItem();
		communes[0].setLabel("--");
		communes[0].setValue(null);
		
		for (FrCommune com : liste) {
			
			communes[i] = new SelectItem(com.getId().getCodeCommune(), com.getNomCommune());
			i++;
			
		}
		
	}
	
	private void viderCommunes() {
		
		List<FrCommune> listeVide = new ArrayList<FrCommune>();
		listerCommunes(listeVide);
		
	}
	
	private void loadCommunes(FrDepartement dept) {
		
		List<FrCommune> listeCommunes = busApp.getCommunes(dept);
		listerCommunes(listeCommunes);
		
	}
	
	private void loadCommunes(long codePostal) {
		
		List<FrCommune> cps = busApp.getCommunes(codePostalId);
		listerCommunes(cps);
		
	}
	
	public void valueChangeCp() {
		
		if (codePostalId != null) {
			
			loadCommunes(codePostalId);
			
		}
		
	}

	private void listerCodesPostaux(List<Integer> liste) {
		
		int i = 1;
		
		Collections.sort(liste);
		
		codePostaux = new SelectItem[liste.size() + 1];
		codePostaux[0] = new SelectItem();
		codePostaux[0].setLabel("--");
		codePostaux[0].setValue(null);
		
		for (int cp : liste) {
			
			String cpString = String.valueOf(cp);
			
			if (cpString.length() < 5) {
				
				cpString = "0" + cpString;
				
			}
			
			codePostaux[i] = new SelectItem(cp, cpString);
			i++;
			
		}
		
	}
	
	private void viderCodesPostaux() {
		
		List<Integer> listeVide = new ArrayList<Integer>();
		listerCodesPostaux(listeVide);
		
	}
	
	private void loadCodesPostaux(FrDepartement dept) {
		
		List<Integer> listeCP = busApp.getCodesPostaux(dept);
		listerCodesPostaux(listeCP);
		
	}
	
	private void loadCodesPostaux(FrCommune comm) {
		
		List<Integer> cps = busApp.getCodesPostaux(comm);
		listerCodesPostaux(cps);
		
	}
	
	public String rechercher() {
		
		
		String[] rechercheString;
		
		if (motCleRecherche != null & motCleRecherche != ""){
			
			rechercheString = motCleRecherche.split("\\s+");
			
		}
		else {
			
			rechercheString = new String[0];
			
		}
		
		FrDepartement rechercheDept = new FrDepartement();
		
			if (departementId != null && departementId != "") {
			
				rechercheDept.setIdDepartement(departementId);
			
			}
			else {
				
				rechercheDept = null;
				
			}
			
		FrRegion rechercheReg = new FrRegion();
		
			if (regionId != null && regionId > 0) {
				
				rechercheReg.setIdRegion(regionId);
				
			}
			else {
				
				rechercheReg = null;
				
			}
		
		Date rechercheDeb = new Date();
		
			if (dateDe != null) {
				
				rechercheDeb = dateDe;
				
			}
			else {
				
				rechercheDeb = null;
				
			}
			
		Date rechercheFin = new Date();
			
			if (dateA != null) {
				
				rechercheFin = dateA;
				
			}
			else {
				
				rechercheFin = null;
				
			}
			
		CategorieAem rechercheCat = new CategorieAem();
		
			if (categorieId != null && categorieId > 0) {
				
				rechercheCat.setIdCategorieAem(categorieId);
				
			}
			else {
				
				rechercheCat = null;
				
			}

		Valeur rechercheVal = new Valeur();
		
			if (valMax != null && valMax > 0) {
				
				rechercheVal.setValeurJournaliere(new BigDecimal(valMax));
				
			}
			else {
				
				rechercheVal = null;
				
			}
		
		List<Aem> aemsBusiness = busRec.rechercherAems(rechercheString, 
													   rechercheReg, 
													   rechercheDept, 
													   null, 
													   null, 
													   rechercheDeb, 
													   rechercheFin, 
													   rechercheCat, 
													   rechercheVal);
		
		aems = new ArrayList<AEMView>();
		
		for (Aem aem : aemsBusiness) {
			
			AEMView av = new AEMView();
			av.setAem(aem);
			aems.add(av);
			
		} 
		
//		bannGeo = getBannGeo();
		
		return "";
		
	}	
		
	public String getFiche(int idAemVoulu) {
		
		//System.out.println(idAemVoulu);
		return "ficheaem";
		
	}

	public int getAemAficher() {
		return aemAficher;
	}

	public void setAemAficher(int aemAficher) {
		this.aemAficher = aemAficher;
	}

	public void recupIdAem(ActionEvent e) {
		
		aemAficher = Integer.parseInt(e.getComponent().getAttributes().get("idAem").toString());
		
	}
	public String affiche(){
		
		return "ficheaem";
		
	}
}
